﻿//#region Loading cycle in center page

var Canvas;
function ShowLoadingCanvas() {
    Canvas = new CanvasLoader('LoadingCenter');
    Canvas.setColor('#6e7387'); // default is '#000000'
    Canvas.setDiameter(45); // default is 40
    Canvas.setDensity(69); // default is 40
    Canvas.setRange(0.9); // default is 1.3
    Canvas.setFPS(46); // default is 24
    Canvas.show(); // Hidden by default
    //setTimeout(function () { cl.kill() }, 3000);
    SetPositionLoadingCanvas();
}
function KillLoadingCanvas() {
    $("div[id^='canvasLoader']").remove();
}
function SetPositionLoadingCanvas() {
    var left = $('#MasterDivContentCenter').offset().left
    var top = $('#MasterDivContentCenter').offset().top
    var width = $('#MasterDivContentCenter').width();
    var height = $('#MasterDivContentCenter').height();
    var finalLeft = left + width / 2 - 22.5;
    var finalTop = top + height / 2 - 22.5;

    var alldiv = $("div[id^='canvasLoader']");
    for (var i = 0; i < alldiv.length; i++) {
        var loaderObj = alldiv[i];
        loaderObj.style.position = "absolute";
        //loaderObj.style["top"] = cl.getDiameter() * 10 + "px";
        //loaderObj.style["left"] = cl.getDiameter() * 10 + "px";
        loaderObj.style["top"] = finalTop + "px";
        loaderObj.style["left"] = finalLeft + "px";
    }
}
//#endregion 

function ShowPopup(idElement) {

    //        $(e).bPopup();
    //============================
    //        $(e).bPopup({
    //            follow: [false, false], //x, y
    //            position: [150, 400] //x, y
    //        });
    //============================
    //        $('#txt').bPopup({
    //            modalClose: false,
    //            opacity: 0.6,
    //            positionStyle: 'fixed' //'fixed' or 'absolute'
    //        });
    //        =================================
    //        $('#txt').bPopup({
    //            easing: 'easeOutBack', //uses jQuery easing plugin
    //            speed: 450,
    //            transition: 'slideDown'
    //        });
    //        ===================================
    // Div_Login_Popup

    $("#" + idElement).bPopup({
        speed: 500,
        transition: 'slideIn'
    });


}
function signInClick(userNameID, PassWordID, divloadID) {
    $("div[id^='Div_Login_Popup']").filter(function () { return $(this).css("display") == "none" }).remove();
    var UserName = $("#" + userNameID).val();
    var PassWord = $("#" + PassWordID).val();

    $.ajax({
        url: '/User/LoginAction',
        type: 'POST',
        async: false,
        data: { UserName: UserName, PassWord: PassWord },
        success: function (result) {
            if (result == "") {
            } else {
                //$("span[id^='spanClose']").click();
                $("#spanClose").first().click();
                // $("div[id^='Div_Login_Popup'] [style='display:none'] ").remove();


                $("#" + divloadID).html(result);
                reloadMenu();
            }
        }
    });
}
function logoutClick(divloadID) {

    $.ajax({
        url: '/User/LogoutAction',
        type: 'POST',
        async: false,
        data: {},
        success: function (result) {
            if (result == "") {
            } else {
                $("#" + divloadID).html(result);
                reloadMenu();
            }
        }
    });

}

function Account_registerClick(UserNameRegister, PasswordRegister, PasswordAgainRegister, PasswordAgainError, UsernameRegisterError) {
    /// <summary>function to register</summary>
    /// <param name="UserNameRegister" type="Text">ID textbox username register</param>
    /// <param name="PasswordRegister" type="Text">ID textbox password register</param>
    /// <param name="PasswordAgainRegister" type="Text">ID textbox password again register</param>
    /// <param name="PasswordAgainError" type="Text">ID textbox password again error to set text when error</param>
    /// <param name="UsernameRegisterError" type="Text">ID textbox username error to set text when error</param>
    /// <returns type="">The area.</returns>

    $("div[id^='Div_Register_Popup']").filter(function () { return $(this).css("display") == "none" }).remove();
  //  $("#spanCloseRegister").click();

    var UsernameElement = $("#" + UserNameRegister).val();
    var PasswordElement = $("#" + PasswordRegister).val();
    var PasswordAgainElement = $("#" + PasswordAgainRegister).val();
    if (PasswordElement != PasswordAgainElement) {
        
        $("#" + PasswordAgainError).html("Password again is wrong!")
        return;
    }
    $.ajax({
        url: '/User/RegisterAction',
        type: 'POST',

        async: false,
        data: { UserName: UsernameElement, PassWord: PasswordElement },
        success: function (result) {
            if (result.result_bool == false) {
                $("#" + PasswordAgainError).html(result.result_Str)
            }
            else {
            //thành công thì phải load luon wellcome bên dưới và show 1 message báo là đăng ký thàng công
                $("#spanCloseRegister").click();
                $("#spanClose").first().click();
                $("#" + divloadID).html(result);
                reloadMenu();
            }
        }
    });
}

function checkEnterToLogin(e) {
    if (e.keyCode == 13) {
        $("#btn_SignIn").click();
    }

}
function reloadMenu() {

    $.ajax({
        url: '/Menu/ReloadMenu',
        type: 'POST',
        async: false,
        data: {},
        success: function (result) {
            if (result == "") {
            } else {
                $("#Div_menu").html(result);
            }
        }
    });
}
function redirectTolink(link) {
    ShowLoadingCanvas();
    var resultLink = "";
    var arr = link.split('|');
    for (var i = 0; i < arr.length; i++) {
        resultLink += arr[i] + "/";
    }
    $.ajax({
        url: resultLink,
        type: 'POST',
        async: true,
        data: {},
        success: function (result) {
            if (result != "")
                $("#Div_UserControlContent").html(result);
            KillLoadingCanvas();
        },
        error: function (xhr, textStatus, thrownError) {
            KillLoadingCanvas();
        }
    });

}


  